﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MobileShop.Controls;

namespace MobileShop
{
    public partial class SearchPrd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lstProduct.Rebind();
                if (CategoryID > 0)
                {
                    View_CategoryTree c = new View_CategoryTree();
                    c.Where.ID.Value = CategoryID;
                    c.Query.Load();
                    string str = "";
                    string[] pathNames = c.s_Path.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
                    string[] pathIDs = c.s_IDPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < pathNames.Length; i++)
                    {
                        str += string.Format("{1}[{0}]", this.ResolveUrl(string.Format("~/Search.aspx?category={0}_{1}", Utilities.GetValidParam(pathNames[i]), pathIDs[i])), pathNames[i]);
                        if (i < pathNames.Length - 1)
                            str += "|";
                    }

                    PageMap = string.Format("Product[{0}]|{1}", this.ResolveUrl("~/Search.aspx?category=All_0"), str);
                }
                else if (!string.IsNullOrEmpty(Request.QueryString["product"]))
                {
                    string prod = Request.QueryString["product"];
                    if (prod == "new_arrival")
                    {
                        PageMap = string.Format("New Arrival[{0}]", this.ResolveUrl("~/Search.aspx?product=new_arrival"));
                    }
                    else if (prod == "daily_special")
                    {
                        PageMap = string.Format("Daily Special[{0}]", this.ResolveUrl("~/Search.aspx?product=daily_special"));
                    }
                }
            }
        }

        protected int CategoryID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["category"]))
                {
                    return MobileShop.Utilities.GetParmValue(Request.QueryString["category"]);
                }
                return -1;
            }
        }

        protected void lstProduct_OnNeedDataSource(object sender, EventArgs e)
        {
            string filter = "";

            if (CategoryID > 0)
            {
                filter = View_WebProducts.ColumnNames.CategoryID + "=" + CategoryID.ToString();
            }

            else if (!string.IsNullOrEmpty(Request.QueryString["product"]))
            {
                string prod = Request.QueryString["product"];
                if (prod == "new_arrival")
                {
                    filter = View_WebProducts.ColumnNames.IsNewArrival + "=1";
                }
                else if (prod == "daily_special")
                {
                    filter = View_WebProducts.ColumnNames.IsHotProduct + "=1";
                }
            }

            if (!string.IsNullOrEmpty(Request.QueryString["key"]))
            {
                filter = View_WebProducts.ColumnNames.ProductName + " LIKE '%" + Request.QueryString["key"] + "%' OR " + View_WebProducts.ColumnNames.CategoryName + " LIKE '%" + Request.QueryString["key"] + "%'";
            }

            if (View_Customers.GetCurrentUser() == null || View_Customers.GetCurrentUser().DealerPrice == false)
            {
                if (filter != "")
                    filter += " and " + View_WebProducts.ColumnNames.IsDealerOnly + " != 1";
                else
                    filter = View_WebProducts.ColumnNames.IsDealerOnly + " != 1";
            }

            View_WebProducts prods = new View_WebProducts();
            int count = prods.Search(filter, "[ID] desc", dataPager.CurrentPageIndex * dataPager.PageSize, dataPager.PageSize);
            if (count > 0)
                lstProduct.DataSource = prods.DefaultView;
            else
                lstProduct.DataSource = null;
            dataPager.TotalItems = count;
            lblNoItems.Visible = count <= 0;
        }

        protected void dataPager_OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            lstProduct.Rebind();
        }
    }
}